package com.clgc.eduservice.service.impl;

import com.clgc.eduservice.entity.EduTopic;
import com.clgc.eduservice.entity.StudentTopic;
import com.clgc.eduservice.mapper.StudentTopicMapper;
import com.clgc.eduservice.service.EduTopicService;
import com.clgc.eduservice.service.StudentTopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 学生和题表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@Service
public class StudentTopicServiceImpl extends ServiceImpl<StudentTopicMapper, StudentTopic> implements StudentTopicService {

    @Autowired
    EduTopicService topicService;

    //提交每一道题并修改客观题部分，将修改后的题信息添加到数据库
    @Override
    public boolean subTopic(StudentTopic studentTopic) {
        String topicId = studentTopic.getTopicId();
        EduTopic eduTopic = topicService.getById(topicId);
        if (eduTopic.getKind().equals("单选题")
                || eduTopic.getKind().equals("多选题")
                || eduTopic.getKind().equals("判断题")
        ) {
            switch (eduTopic.getKind()) {
                case "单选题": {
                    if (studentTopic.getAnswer().equals(eduTopic.getAnswer())) {
                        studentTopic.setSituation("正确");
                    } else
                        studentTopic.setSituation("错误");
                    break;
                }
                case "多选题": {
                    if (studentTopic.getAnswer().equals(eduTopic.getAnswer())) {
                        studentTopic.setSituation("正确");
                    } else
                        studentTopic.setSituation("错误");
                    break;
                }
                case "判断题": {
                    if (eduTopic.getAnswer().equals("A") || eduTopic.getAnswer().equals("对") ||
                            eduTopic.getAnswer().equals(("✔"))) {
                        eduTopic.setAnswer("对");
                    } else
                        eduTopic.setAnswer("错");

                    if (studentTopic.getAnswer().equals(eduTopic.getAnswer())) {
                        studentTopic.setSituation("正确");
                    } else
                        studentTopic.setSituation("错误");
                    break;
                }
            }
        }

        int flag = baseMapper.insert(studentTopic);

        return flag > 0 ? true : false;
    }
}
